'use strict';
let uniID = require('uni-id')
const db = uniCloud.database();
const integral = db.collection('integral');
exports.main = async (event, context) => {
	uniID = uniID.createInstance({
		context
	})
	let params = event.params || {}
	let payload = {}
	var data_eve = {}
	if (!event.uniIdToken) {
		return {
			code: 403,
			msg: '缺少token'
		}
	}
	payload = await uniID.checkToken(event.uniIdToken, {
		needPermission: true
	})
	if (payload.code && payload.code > 0) {
		return payload
	}
	params.uid = payload.uid
	const {
		userInfo,
	} = await uniID.checkToken(event.uniIdToken, {
		needPermission: true
	})

	const {
		uid,
	} = params

	const {
		data
	} = await integral.where({
		uid: uid
	}).get()
	
	return {
		code: 0,
		data: data.reverse()
	}
};
